from xml.dom.minidom import Document
import logging
import types

BEGIN,\
PASS,\
FAILURE,\
ERROR,\
NORUN,\
END = range(6)

class CaseResultInfo():
    def __init__(self):
        self.name = None
        self.result = None
        self.message = ''
        self.start_time = None
        self.end_time = None
        
def GeneralResultOuput(filename, resultlist):
    doc = Document()
    testcases = doc.createElement('testsuite')
    doc.appendChild(testcases)
    
    for v in resultlist:    
        testcase = doc.createElement('testcase')
        testcases.appendChild(testcase)
        logging.info(type(v.name))
        if type(v.name) != types.UnicodeType:
            v.name = unicode(v.name, 'GB18030')
        testcase.setAttribute('name', v.name.encode('utf-8'))
        testcase.setAttribute('start_time', v.start_time)
        testcase.setAttribute('end_time', v.end_time)
        logging.info(type(v.message))
        if v.message  and type(v.name) != types.UnicodeType:
            v.message = unicode(v.message, 'GB18030')
        if v.result == FAILURE:
            failure = doc.createElement('failure')
            testcase.appendChild(failure)
            failure.setAttribute('message', v.message.encode('utf-8'))
        elif v.result == ERROR:
            error = doc.createElement('error')
            testcase.appendChild(error)
            error.setAttribute('message', v.message.encode('utf-8'))
        
    f = open(filename, 'w')
    doc.writexml(f, indent="\t", newl="\n", encoding='utf-8')
    f.close()

